<template>
  <div class="search-box">
    <!-- 位置信息 -->
    <div class="location bottom-gray-line">
      <div class="city" @click="cityClick">{{ currentCity.cityName }}</div>
      <div class="position" @click="positionClick">
        <span class="text">我的位置</span>
        <van-icon name="aim" color="orange" />
      </div>
    </div>
    <!-- 日期范围 -->
    <div
      class="section date-range bottom-gray-line"
      @click="showCalendar = true"
    >
      <div class="start">
        <div class="date">
          <span class="tip">入住</span>
          <span class="time">{{ startDateStr }}</span>
        </div>
      </div>
      <div class="stay">共{{ stayCount }}晚</div>
      <div class="end">
        <div class="date">
          <span class="tip">离店</span>
          <span class="time">{{ endDateStr }}</span>
        </div>
      </div>
    </div>
    <van-calendar
      v-model:show="showCalendar"
      :round="false"
      color="#f4ea2b"
      type="range"
      @confirm="onConfirm"
    />

    <!-- 价格人数选择 -->
    <div class="section price-counter bottom-gray-line">
      <div class="start">价格不限</div>
      <div class="end">人数不限</div>
    </div>
    <div class="section keyword bottom-gray-line">关键字/位置/民宿名</div>

    <!-- 热门建议 -->
    <div class="section hot-suggest">
      <template v-for="(item, index) in hotSuggests" :key="index">
        <div
          class="item"
          :style="{
            color: item.tagText.color,
            background: item.tagText.background.color,
          }"
        >
          {{ item.tagText.text }}
        </div>
      </template>
    </div>

    <!-- 搜索按钮 -->
    <div class=" search-btn">
      <div class="btn" @click="searchBtnClick">开始搜索</div>
    </div> 
  </div>
</template>
<script setup>
import { ref, computed } from "vue";
import useCityStore from "@/store/modules/city";
import { useRouter } from "vue-router";
import { formatMonthDay, getDiffDays } from "@/utils/format_date.js";
import useHomeStore from "@/store/modules/home";
import useMainStore from "@/store/modules/main";
import { storeToRefs } from "pinia";
const storeHome = useHomeStore();
const { hotSuggests } = storeToRefs(storeHome);
// 位置/城市
const cityStore = useCityStore();
const { currentCity } = cityStore;
const router = useRouter();
const cityClick = () => {
  router.push("/city");
};

// 获取位置
const positionClick = () => {
  navigator.geolocation.getCurrentPosition(
    (res) => {
      console.log("获取位置成功", res);
    },
    (err) => {
      console.log("获取位置失败", err);
    },
    {
      enableHighAccuracy: true,
      timeout: 5000,
      maximumAge: 0,
    }
  );
};

// 日期范围

const mainStore = useMainStore()
const {startDate , endDate} = storeToRefs(mainStore)

const startDateStr = computed(()=> formatMonthDay(startDate.value));
const endDateStr = computed(()=> formatMonthDay(endDate.value));
const stayCount = ref(getDiffDays(startDate.value, endDate.value));
const showCalendar = ref(false);
const onConfirm = (value) => {
  const selectStart = value[0];
  const selectEnd = value[1];
  mainStore.startDate = selectStart
  mainStore.endDate = selectEnd
  stayCount.value = getDiffDays(selectStart, selectEnd);
  // 隐藏
  showCalendar.value = false;

};

// 开始搜索
const searchBtnClick = ()=>{
  router.push(
    {
      path: "/search",
      query:{
        startDate: startDateStr,
        endDate: endDateStr,
        currentCity: currentCity.cityName
      }
    }
  )
}
</script>
<style lang="less" scoped>
/*局部css 支持css3 可以改变elementUI组件的样式 且组件样式调整只在本页面生效**/
/*主样式*/
.search-box {
  --van-calendar-popup-height: 100%;
}
.location {
  display: flex;
  align-items: center;
  height: 44px;
  padding: 0 20px;
  .city {
    flex: 1;
    color: #333;
    font-size: 15px;
  }
  .position {
    width: 74px;
    .text {
      font-size: 12px;
      margin-right: 5px;
      width: 18px;
      height: 18px;
    }
  }
}
.date-range {
  height: 44px;
  .stay {
    flex: 1;
    text-align: center;
    font-size: 12px;
    color: #666;
  }
}
.section {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  padding: 0 20px;
  color: #999;
  
  .start {
    flex: 1;
    display: flex;
    height: 44px;
    align-items: center;
  }
  .end {
    min-width: 30%;
    padding-left: 20px;
  }
  .date {
    display: flex;
    flex-direction: column;
    .tip {
      font-size: 12px;
      color: #999;
    }
    .time {
      margin-top: 3px;
      color: #333;
      font-size: 15px;
      font-weight: 500;
    }
  }
}
.price-counter {
  .start {
    border-right: 1px solid var(--line-color);
  }
}
.hot-suggest {
  margin: 10px 0;
  .item {
    padding: 4px 8px;
    border-radius: 14px;
    margin: 4px;
    font-size: 12px;
    line-height: 1;
  }
}
.search-btn {
  display: flex;
  justify-content: center;
  text-align: center;
  .btn {
    width: 342px;
    height: 38px;
    max-height: 50px;
    font-weight: 500;
    font-size: 18px;
    line-height: 38px;
    text-align: center;
    border-radius: 20px;
    color: #fff;
    background-color: red;

    background-image: var(--theme-linear-gradient);
  }
}
</style>
